package java第六次作业;


import java.util.stream.Collectors;
import java.util.ArrayList;
import java.util.Collections;  
import java.util.Comparator;  
import java.util.HashMap;
import java.util.List;
import java.util.stream.Stream;  

public class DouDiZhu {  
    public static void main(String[] args) {  
        // 准备牌  
        ArrayList<String> poker = new ArrayList<>();  
        String[] colors = {"♠", "♣", "♦", "♥"};  
        String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};  
        poker.add("大王");  
        poker.add("小王");  

        for (String number : numbers) {  
            for (String color : colors) {  
                poker.add(color + number);  
            }  
        }  

        // 打印生成的牌  
        System.out.println(poker);  
        System.out.println("========");  

        // 洗牌  
        Collections.shuffle(poker);  
        System.out.println(poker);  

        // 玩家创建  
        Player p1 = new Player();  
        p1.setName("老王");  
        Player p2 = new Player();  
        p2.setName("老李");  
        Player p3 = new Player();  
        p3.setName("老六");  
        ArrayList<String> dipai = new ArrayList<>();  

        // 发牌  
        for (int i = 0; i < poker.size(); i++) {  
            String p = poker.get(i);  
            if (i >= 51) {  
                dipai.add(p);  
            } else if (i % 3 == 0) {  
                p1.getPoker().add(p);  
            } else if (i % 3 == 1) {  
                p2.getPoker().add(p);  
            } else {  
                p3.getPoker().add(p);  
            }  
        }  

     // 定义扑克牌排序规则  
        String[] order = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "小王", "大王"};  
        HashMap<String, Integer> orderMap = new HashMap<>();  
        for (int i = 0; i < order.length; i++) {  
            orderMap.put(order[i], i);  
        }  

        
         
        // 排序  
           List<String> collect = p1.getPoker().stream().sorted(new  Comparator<String>(){
			@Override
			public int compare(String o1, String o2) {
				// TODO Auto-generated method stub
				String num1 = getValue(o1);  
                String num2 = getValue(o2);  
                return orderMap.get(num1) - orderMap.get(num2);  
			}
        }).collect(Collectors.toList());
       
           List<String> collect2 = p2.getPoker().stream().sorted(new  Comparator<String>(){

   			@Override
   			public int compare(String o1, String o2) {
   				// TODO Auto-generated method stub
   				String num1 = getValue(o1);  
                   String num2 = getValue(o2);  
                   return orderMap.get(num1) - orderMap.get(num2);  
   			}
           }).collect(Collectors.toList());
           
           List<String> collect3 = p3.getPoker().stream().sorted(new  Comparator<String>(){

      			@Override
      			public int compare(String o1, String o2) {
      				// TODO Auto-generated method stub
      				String num1 = getValue(o1);  
                      String num2 = getValue(o2);  
                      return orderMap.get(num1) - orderMap.get(num2);  
      			}
              }).collect(Collectors.toList());
           List<String> collect4 = dipai.stream().sorted(new  Comparator<String>(){

     			@Override
     			public int compare(String o1, String o2) {
     				// TODO Auto-generated method stub
     				String num1 = getValue(o1);  
                     String num2 = getValue(o2);  
                     return orderMap.get(num1) - orderMap.get(num2);  
     			}
             }).collect(Collectors.toList());
        
        // 看牌  
        System.out.println(p1.getName() + "的牌:" +collect);  
        System.out.println(p2.getName() + "的牌:" + collect2);  
        System.out.println(p3.getName() + "的牌:" + collect3);  
        System.out.println("底牌:" + collect4 );  
    }  

   

	private static String getValue(String card) {   
        if (card.equals("大王") || card.equals("小王")) {  
            return card;  
        }    
        return card.substring(1);  
    }  
}  

